package com.dianping.cat.consumer.dependency.model.transform;

import com.dianping.cat.consumer.dependency.model.IVisitor;
import com.dianping.cat.consumer.dependency.model.entity.Dependency;
import com.dianping.cat.consumer.dependency.model.entity.DependencyReport;
import com.dianping.cat.consumer.dependency.model.entity.Index;
import com.dianping.cat.consumer.dependency.model.entity.Segment;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

/*  21 */   public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
    this.m_out = new DataOutputStream(out);
/*  26 */     this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(DependencyReport dependencyReport) {
/*  30 */     ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

    build(dependencyReport, out);
/*  33 */     return out.toByteArray();
  }


/*  37 */   public static void build(DependencyReport dependencyReport, OutputStream out) { dependencyReport.accept(new DefaultNativeBuilder(out)); }



  public void visitDependency(Dependency dependency) {
/*  42 */     if (dependency.getType() != null) {
/*  43 */       writeTag(1, 1);
/*  44 */       writeString(dependency.getType());
    }

/*  47 */     if (dependency.getTarget() != null) {
/*  48 */       writeTag(2, 1);
/*  49 */       writeString(dependency.getTarget());
    }

/*  52 */     writeTag(3, 0);
/*  53 */     writeLong(dependency.getTotalCount());

/*  55 */     writeTag(4, 0);
/*  56 */     writeDouble(dependency.getAvg());

/*  58 */     writeTag(5, 0);
/*  59 */     writeLong(dependency.getErrorCount());

/*  61 */     if (dependency.getKey() != null) {
/*  62 */       writeTag(6, 1);
/*  63 */       writeString(dependency.getKey());
    }

/*  66 */     writeTag(7, 0);
/*  67 */     writeDouble(dependency.getSum());

/*  69 */     writeTag(63, 3);
  }


  public void visitDependencyReport(DependencyReport dependencyReport) {
/*  74 */     writeTag(63, 0);

/*  76 */     if (dependencyReport.getDomain() != null) {
/*  77 */       writeTag(1, 1);
      writeString(dependencyReport.getDomain());
    }

/*  81 */     if (dependencyReport.getStartTime() != null) {
/*  82 */       writeTag(2, 1);
/*  83 */       writeDate(dependencyReport.getStartTime());
    }

/*  86 */     if (dependencyReport.getEndTime() != null) {
/*  87 */       writeTag(3, 1);
/*  88 */       writeDate(dependencyReport.getEndTime());
    }

/*  91 */     if (dependencyReport.getDomainNames() != null) {
/*  92 */       writeTag(4, 2);
/*  93 */       writeInt(dependencyReport.getDomainNames().size());

/*  95 */       for (String domainName : dependencyReport.getDomainNames()) {
/*  96 */         writeString(domainName);
      }
    }

/* 100 */     if (!dependencyReport.getSegments().isEmpty()) {
/* 101 */       writeTag(33, 2);
/* 102 */       writeInt(dependencyReport.getSegments().size());

/* 104 */       for (Segment segment : dependencyReport.getSegments().values()) {
/* 105 */         segment.accept(this.m_visitor);
      }
    }

/* 109 */     writeTag(63, 3);
  }


  public void visitIndex(Index index) {
/* 114 */     if (index.getName() != null) {
/* 115 */       writeTag(1, 1);
/* 116 */       writeString(index.getName());
    }

/* 119 */     writeTag(2, 0);
/* 120 */     writeLong(index.getTotalCount());

/* 122 */     writeTag(3, 0);
/* 123 */     writeLong(index.getErrorCount());

/* 125 */     writeTag(4, 0);
/* 126 */     writeDouble(index.getSum());

/* 128 */     writeTag(5, 0);
/* 129 */     writeDouble(index.getAvg());

/* 131 */     writeTag(63, 3);
  }


  public void visitSegment(Segment segment) {
/* 136 */     if (segment.getId() != null) {
/* 137 */       writeTag(1, 1);
/* 138 */       writeInt(segment.getId().intValue());
    }

/* 141 */     if (segment.getExceptionCount() != null) {
/* 142 */       writeTag(2, 1);
/* 143 */       writeInt(segment.getExceptionCount().intValue());
    }

/* 146 */     if (!segment.getIndexs().isEmpty()) {
/* 147 */       writeTag(33, 2);
/* 148 */       writeInt(segment.getIndexs().size());

/* 150 */       for (Index index : segment.getIndexs().values()) {
/* 151 */         index.accept(this.m_visitor);
      }
    }

/* 155 */     if (!segment.getDependencies().isEmpty()) {
/* 156 */       writeTag(34, 2);
/* 157 */       writeInt(segment.getDependencies().size());

/* 159 */       for (Dependency dependency : segment.getDependencies().values()) {
/* 160 */         dependency.accept(this.m_visitor);
      }
    }

/* 164 */     writeTag(63, 3);
  }

  private void writeDate(Date value) {
    try {
/* 169 */       writeVarint(value.getTime());
/* 170 */     } catch (Exception e) {
/* 171 */       throw new RuntimeException(e);
    }
  }

  private void writeDouble(double value) {
    try {
/* 177 */       writeVarint(Double.doubleToLongBits(value));
/* 178 */     } catch (Exception e) {
/* 179 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 185 */       writeVarint(value & 0xFFFFFFFFL);
/* 186 */     } catch (Exception e) {
/* 187 */       throw new RuntimeException(e);
    }
  }

  private void writeLong(long value) {
    try {
/* 193 */       writeVarint(value);
/* 194 */     } catch (Exception e) {
/* 195 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 201 */       this.m_out.writeUTF(value);
/* 202 */     } catch (IOException e) {
/* 203 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 209 */       this.m_out.writeByte((field << 2) + type);
/* 210 */     } catch (IOException e) {
/* 211 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 217 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 218 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 221 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 222 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/dependency/model/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */